package com.che.software.testato.util.jfreechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import com.che.software.testato.domain.entity.MatrixResult;
import com.che.software.testato.util.ColorUtil;
import com.che.software.testato.util.jsf.faces.LocaleUtil;

/**
 * Static class used to colorize the JFree line charts.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see Serializable.
 * @since August, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
public class LineChartGraphistUtil implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(LineChartGraphistUtil.class);
	private static final long serialVersionUID = -8139259349522463698L;

	/**
	 * Private default builder; this is a static class.
	 */
	private LineChartGraphistUtil() {
	}

	/**
	 * Colorized and transforms a given JFreeChart.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param lineChart the given chart.
	 * @param maxAbscissaValue the max abscissa value of the chart.
	 * @param xValues the x axis values list.
	 * @param yValues the y axis values list.
	 * @param xExcludedValues the x axis excluded values.
	 * @param yExcludedValues the y axis excluded values.
	 * @return the transformed chart.
	 * @since August, 2011.
	 */
	public static JFreeChart getColorizedChartFromChart(JFreeChart lineChart, double maxAbscissaValue, List<MatrixResult> xValues, List<MatrixResult> yValues, List<MatrixResult> xExcludedValues, List<MatrixResult> yExcludedValues) {
		LOGGER.debug("getColorizedChartFromChart().");
		lineChart.setBackgroundPaint(Color.WHITE);
		XYPlot plot = (XYPlot) lineChart.getPlot();
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, false);
		renderer.setSeriesLinesVisible(1, false);
		renderer.setSeriesStroke(2, new BasicStroke(2));
		renderer.setSeriesStroke(3, new BasicStroke(2));
		renderer.setSeriesShapesVisible(2, false);
		renderer.setSeriesShapesVisible(3, false);
		renderer.setSeriesPaint(0, ColorUtil.getHSBColor(ColorUtil.BLUE_COLOR[0], ColorUtil.BLUE_COLOR[1], ColorUtil.BLUE_COLOR[2]));
		renderer.setSeriesPaint(1, ColorUtil.getHSBColor(ColorUtil.ORANGE_COLOR[0], ColorUtil.ORANGE_COLOR[1], ColorUtil.ORANGE_COLOR[2]));
		renderer.setSeriesPaint(2, ColorUtil.getHSBColor(ColorUtil.DARK_BLUE_COLOR[0], ColorUtil.DARK_BLUE_COLOR[1], ColorUtil.DARK_BLUE_COLOR[2]));
		renderer.setSeriesPaint(3, ColorUtil.getHSBColor(ColorUtil.DARK_BLUE_COLOR[0], ColorUtil.DARK_BLUE_COLOR[1], ColorUtil.DARK_BLUE_COLOR[2]));
		plot.setRenderer(renderer);
		XYTextAnnotation low = new XYTextAnnotation(LocaleUtil.getResourceBundleStringByName(LocaleUtil.AREA_LOW_NAME), (0.5 * maxAbscissaValue) - 1, maxAbscissaValue + 1), medium = new XYTextAnnotation(LocaleUtil.getResourceBundleStringByName(LocaleUtil.AREA_MEDIUM_NAME), (1.5 * maxAbscissaValue) - 2, maxAbscissaValue + 1), high = new XYTextAnnotation(LocaleUtil.getResourceBundleStringByName(LocaleUtil.AREA_HIGH_NAME), 1.5 * maxAbscissaValue, maxAbscissaValue - 2);
		low.setFont(ColorUtil.TITLE_FONT);
		medium.setFont(ColorUtil.TITLE_FONT);
		high.setFont(ColorUtil.TITLE_FONT);
		low.setPaint(ColorUtil.getHSBColor(ColorUtil.DARK_BLUE_COLOR[0], ColorUtil.DARK_BLUE_COLOR[1], ColorUtil.DARK_BLUE_COLOR[2]));
		medium.setPaint(ColorUtil.getHSBColor(ColorUtil.DARK_BLUE_COLOR[0], ColorUtil.DARK_BLUE_COLOR[1], ColorUtil.DARK_BLUE_COLOR[2]));
		high.setPaint(ColorUtil.getHSBColor(ColorUtil.DARK_BLUE_COLOR[0], ColorUtil.DARK_BLUE_COLOR[1], ColorUtil.DARK_BLUE_COLOR[2]));
		plot.addAnnotation(low);
		plot.addAnnotation(medium);
		plot.addAnnotation(high);
		for (int i = 0; i < xValues.size(); i++) {
			XYTextAnnotation itemLabel = new XYTextAnnotation(xValues.get(i).getScriptLabel(), (yValues.get(i).getPercentage() * 100), (xValues.get(i).getPercentage() * 100) + 0.5);
			itemLabel.setFont(ColorUtil.DEFAULT_FONT);
			itemLabel.setPaint(ColorUtil.getHSBColor(ColorUtil.BLUE_COLOR[0], ColorUtil.BLUE_COLOR[1], ColorUtil.BLUE_COLOR[2]));
			plot.addAnnotation(itemLabel);
		}
		if (null != xExcludedValues) {
			for (int j = 0; j < xExcludedValues.size(); j++) {
				XYTextAnnotation itemLabel = new XYTextAnnotation(xExcludedValues.get(j).getScriptLabel(), (yExcludedValues.get(j).getPercentage() * 100), (xExcludedValues.get(j).getPercentage() * 100) + 0.5);
				itemLabel.setFont(ColorUtil.DEFAULT_FONT);
				itemLabel.setPaint(ColorUtil.getHSBColor(ColorUtil.ORANGE_COLOR[0], ColorUtil.ORANGE_COLOR[1], ColorUtil.ORANGE_COLOR[2]));
				plot.addAnnotation(itemLabel);
			}
		}
		return lineChart;
	}

	/**
	 * Gets the max abscissa value from the abscissa values (excluded or not).
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param yValues the selected values.
	 * @param yExcludedValues the excluded values.
	 * @return the max value.
	 * @since August, 2011.
	 */
	public static double getMaxAbscissaValue(List<MatrixResult> yValues, List<MatrixResult> yExcludedValues) {
		LOGGER.debug("getMaxAbscissaValue().");
		double maxValue = 0;
		for (MatrixResult yValue : yValues) {
			if (yValue.getPercentage() > maxValue) {
				maxValue = yValue.getPercentage();
			}
		}
		for (MatrixResult yValue : yExcludedValues) {
			if (yValue.getPercentage() > maxValue) {
				maxValue = yValue.getPercentage();
			}
		}
		return maxValue * 100;
	}
}